package com.sugree.twitter.views;

import java.io.IOException;
import java.util.Vector;
import java.util.Enumeration;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;

import com.substanceofcode.utils.TimeUtil;
import com.substanceofcode.utils.Log;
import com.substanceofcode.twitter.model.Status;
import com.sugree.twitter.TwitterController;

public class StatusScreen extends Form implements CommandListener {
	private TwitterController controller;

	private Status status;

	private StringItem textField;
	private Command replyCommand;
	private Command favoriteCommand;
	private Command backCommand;

	public StatusScreen(TwitterController controller) {
		super("");
		this.controller = controller;

		textField = new StringItem("", "");
		append(textField);

		backCommand = new Command("Back", Command.BACK, 1);
		addCommand(backCommand);
		replyCommand = new Command("Reply", Command.SCREEN, 2);
		addCommand(replyCommand);
		favoriteCommand = new Command("Toggle Favorite", Command.SCREEN, 3);
		addCommand(favoriteCommand);

		setCommandListener(this);
	}

	public void setStatus(Status status) {
		String favorited = "";

		this.status = status;
		if (status.getFavorited()) {
			favorited = " *";
		}
		setTitle(status.getScreenName());
		textField.setText(status.getText()+"\n\nabout "+TimeUtil.getTimeInterval(status.getDate())+" ago via "+status.getSource()+favorited);
	}

	public void commandAction(Command cmd, Displayable display) {
		if (cmd == replyCommand) {
			controller.showUpdate("@"+getTitle()+" ");
		} else if (cmd == backCommand) {
			status = null;
			controller.showTimeline();
		} else if (cmd == favoriteCommand) {
			controller.toggleFavorited(status);
		}
	}
}
